package edu.uoc.tdp.pac4.client.Academic;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import edu.uoc.tdp.pac4.beans.Course;
import edu.uoc.tdp.pac4.beans.Group;
import edu.uoc.tdp.pac4.beans.Shift;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.AcademicManagementInterface;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

/**
 * JPanel to show the options relative to groups management
 * @author Laia
 *
 */
/**
 * @author Laia
 * 
 */
public class Group_ViewGroup extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	AcademicManagementInterface academicManagement;
	private JTable table;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	private JPanel panel_1 = new JPanel();
	private JComboBox<String> comboBox;
	private JComboBox<Shift> comboBox_1;
	private JRadioButton rdbtnTots;
	private JRadioButton rdbtnInactiu;
	private JRadioButton tbtActiu;

	ClientConnectionHelper connectionHelper_ = null;

	/**
	 * Builds the JPanel
	 */

	public Group_ViewGroup(ClientConnectionHelper connectionHelper) {
		connectionHelper_ = connectionHelper;
		academicManagement = connectionHelper_.getAcademicManager();

		setBorder(new TitledBorder(null,
				LanguageUtils
						.getMessage("client.management.button.titleViewGroup"),
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(6, 16, 619, 164);
		add(panel);
		panel.setLayout(null);

		JLabel lblNomDelCurs = new JLabel(
				LanguageUtils.getMessage("client.management.button.nameCourse"));
		lblNomDelCurs.setBounds(23, 29, 134, 14);
		panel.add(lblNomDelCurs);

		JLabel lblEstatDelGrup = new JLabel(
				LanguageUtils
						.getMessage("client.management.button.groupStatus"));
		lblEstatDelGrup.setBounds(23, 67, 113, 14);
		panel.add(lblEstatDelGrup);

		comboBox = new JComboBox<String>();
		comboBox.setBounds(167, 26, 120, 20);
		try {
			comboBox.setModel(posarModel());
		} catch (RemoteException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, LanguageUtils
					.getMessage("client.management.error.selecgrup"),
					LanguageUtils.getMessage("client.management.error"),
					JOptionPane.ERROR_MESSAGE);
			
		}
		panel.add(comboBox);

		JLabel lblTorn = new JLabel(
				LanguageUtils.getMessage("client.management.button.shift"));
		lblTorn.setBounds(373, 29, 90, 14);
		panel.add(lblTorn);

		comboBox_1 = new JComboBox<Shift>();
		comboBox_1.setBounds(473, 26, 120, 20);
		DefaultComboBoxModel<Shift> model2 = new DefaultComboBoxModel<Shift>();
		model2.addElement(null);
		model2.addElement(Shift.MORNING);
		model2.addElement(Shift.AFTERNOON);

		comboBox_1.setModel(model2);
		panel.add(comboBox_1);

		rdbtnTots = new JRadioButton(
				LanguageUtils.getMessage("client.management.button.all"));
		rdbtnTots.setSelected(true);
		buttonGroup.add(rdbtnTots);
		rdbtnTots.setBounds(142, 63, 73, 23);
		panel.add(rdbtnTots);

		rdbtnInactiu = new JRadioButton(
				LanguageUtils.getMessage("client.management.button.inactive"));
		buttonGroup.add(rdbtnInactiu);
		rdbtnInactiu.setBounds(289, 63, 81, 23);
		panel.add(rdbtnInactiu);

		tbtActiu = new JRadioButton(
				LanguageUtils.getMessage("client.management.button.active"));
		buttonGroup.add(tbtActiu);
		tbtActiu.setBounds(217, 63, 70, 23);
		panel.add(tbtActiu);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(6, 197, 619, 210);
		add(scrollPane);

		table = new JTable();
		table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
				LanguageUtils.getMessage("client.management.button.id"),
				LanguageUtils
						.getMessage("client.management.button.nameCourse2"),
				LanguageUtils.getMessage("client.management.button.shift"),
				LanguageUtils.getMessage("client.management.button.max2"),
				LanguageUtils.getMessage("client.management.button.startDate"),
				LanguageUtils.getMessage("client.management.button.finalDate"),
				LanguageUtils
						.getMessage("client.management.button.groupStatus") }));
		scrollPane.add(table);
		scrollPane.setViewportView(table);

		JButton btnBuscar = new JButton(
				LanguageUtils.getMessage("client.management.button.search"));
		btnBuscar.setBounds(23, 118, 101, 23);
		panel.add(btnBuscar);
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					cercar();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		});

		JButton btnDesar = new JButton(
				LanguageUtils.getMessage("client.management.button.new"));
		btnDesar.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				Group_SaveGroup saveGroup = new Group_SaveGroup(connectionHelper_);
				saveGroup.setVisible(true);
			}
		});
		panel_1.setLayout(null);
		btnDesar.setBounds(10, 5, 114, 23);
		panel_1.add(btnDesar);

		JButton button = new JButton(
				LanguageUtils.getMessage("client.management.button.clean"));
		panel.add(button);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				comboBox.setSelectedIndex(0);
				comboBox_1.setSelectedIndex(0);
				rdbtnTots.setSelected(true);
				rdbtnInactiu.setSelected(false);
				tbtActiu.setSelected(false);


			}
		});
		button.setBounds(167, 118, 132, 23);

		panel_1.setBounds(6, 424, 619, 37);
		add(panel_1);

		JButton btnAssign = new JButton(
				LanguageUtils
						.getMessage("client.management.button.addResources"));
		btnAssign.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					afegeix();
				} catch (Exception e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, LanguageUtils
							.getMessage("client.management.error.selecgrup"),
							LanguageUtils.getMessage("client.management.error"),
							JOptionPane.ERROR_MESSAGE);
					
				}

			}
		});
		btnAssign.setBounds(134, 5, 153, 23);
		panel_1.add(btnAssign);

		JButton btnUpdate = new JButton(
				LanguageUtils.getMessage("client.management.button.modify"));
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					actualitza();
				} catch (Exception e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, LanguageUtils
							.getMessage("client.management.error.selecgrup"),
							LanguageUtils.getMessage("client.management.error"),
							JOptionPane.ERROR_MESSAGE);
					
				}

			}
		});
		btnUpdate.setBounds(297, 5, 89, 23);
		panel_1.add(btnUpdate);


	}

	/**
	 * Searches and prints groups
	 * 
	 * @throws RemoteException
	 * 
	 */
	public void cercar() throws RemoteException {
		List<Group> groups;

		String name = null;
		name = (String) comboBox.getSelectedItem();
		Shift shift = (Shift) comboBox_1.getSelectedItem();
		Boolean actiu = null;

		if (tbtActiu.isSelected()) {
			actiu = true;
		}
		if (rdbtnInactiu.isSelected()) {
			actiu = false;
		}
		if (rdbtnTots.isSelected()) {
			actiu = null;
		}



		groups = academicManagement.findGroup(name, shift, actiu);

		Object[] columnNames = new String[] {
				LanguageUtils.getMessage("client.management.button.id"),
				LanguageUtils
						.getMessage("client.management.button.nameCourse2"),
				LanguageUtils.getMessage("client.management.button.shift"),
				LanguageUtils.getMessage("client.management.button.max2"),
				LanguageUtils.getMessage("client.management.button.startDate"),
				LanguageUtils.getMessage("client.management.button.finalDate"),
				LanguageUtils
						.getMessage("client.management.button.groupStatus") };
		Object[][] data = new Object[groups.size()][columnNames.length];

		for (int i = 0; i < data.length; i++) {
			Group group = groups.get(i);
			data[i] = new Object[] { group.getGroupId(), group.getName(),
					group.getShift(), group.getMaxStudents(),
					group.getStartDate(), group.getFinalDate(),
					group.activeStatus() };
		}

		TableModel model = new DefaultTableModel(data, columnNames);
		table.setModel(model);
	}

	
	/**
	 * Makes sure that a group is selected and opens a Group_AssignResources
	 * window
	 * @throws RemoteException 
	 */
	public void afegeix() throws RemoteException {

		int i = table.getSelectedRow();
		TableModel model2 = table.getModel();
			Integer identificador = (Integer) model2.getValueAt(i, 0);
			Group group = academicManagement.getGroup(identificador);

			Group_AssignResources assignResources = new Group_AssignResources(
					group, connectionHelper_);
			assignResources.setVisible(true);

	}

	/**
	 * Makes sure that a group is selected and opens a Group_UpdateGroup window
	 * 
	 * @throws RemoteException
	 */
	public void actualitza() throws RemoteException {

		int i = table.getSelectedRow();
		TableModel model2 = table.getModel();
		Integer identificador = (Integer) model2.getValueAt(i, 0);
		Group group = academicManagement.getGroup(identificador);

		Group_UpdateGroup updateGroup = new Group_UpdateGroup(group, connectionHelper_);
		updateGroup.setVisible(true);

	}

	/**
	 * Populates the comboBoxModel with names of courses.
	 * 
	 * @return
	 * @throws RemoteException 
	 */
	
	public DefaultComboBoxModel<String> posarModel() throws RemoteException {
		List<Course> courses;
		courses = academicManagement.listCourses();
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
		model.addElement(null);
		for (int i = 0; i < courses.size(); i++) {
			model.addElement(courses.get(i).getCourseName());
		}
		return model;

	}

}
